<?php
class Cp_GroupController extends Pidguin_Controller_Cp_Action
{
	public function indexAction()
	{
		$this->view->groups = Model_Groups::getInstance()->fetchAll();
	}
	
	public function createAction()
	{
		$this->view->form = new Application_Form_Cp_Group_Create();
		$this->view->resources = Model_Resource::getInstance()->fetchAll();
	}
	
	public function createSaveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
		
		if (!$this->_getParam('key')) {
			throw new Zend_Controller_Action_Exception('Key is not set');
		}
		
		if (!preg_match('/[a-z0-9_]*/i', $this->_getParam('key'))) {
			throw new Zend_Controller_Action_Exception('Key incorrect');
		}
		
		if (!$this->_getParam('title')) {
			throw new Zend_Controller_Action_Exception('Title is not set');
		}
		
		if (!preg_match('/[a-zа-я0-9_\-\.]*/i', $this->_getParam('title'))) {
			throw new Zend_Controller_Action_Exception('Title incorrect');
		}
		
		if (!$this->_getParam('description')) {
			throw new Zend_Controller_Action_Exception('Description is not set');
		}
		
		if (!preg_match('/[a-zа-я0-9_\s\-\.]*/i', $this->_getParam('description'))) {
			throw new Zend_Controller_Action_Exception('Description incorrect');
		}
		
		if (!$this->_getParam('access')) {
			throw new Zend_Controller_Action_Exception('Access is not set');
		}
		
		$gm = Model_Groups::getInstance();
		$select = $gm->select()
			->where('name = ?', $this->_getParam('key'));
		$gr = $gm->fetchRow($select);
		
		if ($gr) {
			throw new Zend_Controller_Action_Exception('Group ' . $this->_getParam('key') . ' is already created');
		}
		
		$gr = $gm->createRow();
		$gr->name = $this->_getParam('key');
		$gr->title = $this->_getParam('title');
		$gr->description = $this->_getParam('description');
		$gr->admin = Pidguin_Client::getInstance()->getClient()->user->id;
		$gr->role_id = 3;
		$gr->created = date('Y-m-d H:i:s');
		
		$rm = Model_Resource::getInstance();
		foreach ($this->_getParam('access') as $resourceId) {
			$select = $rm
				->select()
				->where('id = ?', $resourceId);
			$rr = $rm->fetchRow($select);
			if (!$rr) {
				throw new Zend_Controller_Action_Exception('Resource not found');
			}
		}
		
		$gr->save();
		$am = Model_Access::getInstance();
		foreach ($this->_getParam('access') as $resourceId) {
			$row = $am->createRow();
			$row->group_id = $gr->id;
			$row->resource_id = $resourceId;
			$row->allow = 1;
			$row->active = 1;
			$row->save();
		}
		$this->_helper->json(array('status' => 'ok'));
	}
	
	public function deleteAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Group id is not set');
		}
		
		if ($this->_getParam('id') == '1' || 
				$this->_getParam('id') == '2' ||
				$this->_getParam('id') == '3' ||
				$this->_getParam('id') == '4') {
			throw new Zend_Controller_Action_Exception('You can not delete system groups');
		}
	
		$m = Model_Groups::getInstance();
		$select = $m->select()->where(Model_Groups::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Group not found');
		}
		$row->delete();
		$this->_redirect('/cp/group');
	}
	
	public function editAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Group ID is not set');
		}
		 
		$m = Model_Groups::getInstance();
		$select = $m->select()->where(Model_Groups::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		 
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Group not found');
		}
		 
		$_access = $row->findDependentRowset('Model_Access');
		$accessId = array();
		if (count($_access)) {
			foreach ($_access as $access) {
				$_r = $access->findParentRow('Model_Resource');
				$accessId[] = array('resourceId' => $_r->id);
			}
		}
		$this->view->group = $row;
		$this->view->access = Zend_Json::encode($accessId);
		$this->view->resources = Model_Resource::getInstance()->fetchAll();
	}
	
	public function editSaveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
	
		if (!$this->_getParam('key')) {
			throw new Zend_Controller_Action_Exception('Key is not set');
		}
	
		if (!preg_match('/[a-z0-9_]*/i', $this->_getParam('key'))) {
			throw new Zend_Controller_Action_Exception('Key incorrect');
		}
	
		if (!$this->_getParam('title')) {
			throw new Zend_Controller_Action_Exception('Title is not set');
		}
	
		if (!preg_match('/[a-zа-я0-9_\-\.]*/i', $this->_getParam('title'))) {
			throw new Zend_Controller_Action_Exception('Title incorrect');
		}
	
		if (!$this->_getParam('description')) {
			throw new Zend_Controller_Action_Exception('Description is not set');
		}
	
		if (!preg_match('/[a-zа-я0-9_\s\-\.]*/i', $this->_getParam('description'))) {
			throw new Zend_Controller_Action_Exception('Description incorrect');
		}
	
		if (!$this->_getParam('access')) {
			throw new Zend_Controller_Action_Exception('Access is not set');
		}
	
		$gm = Model_Groups::getInstance();
		$select = $gm->select()
		->where('name = ?', $this->_getParam('key'));
		$gr = $gm->fetchRow($select);
	
		if (!$gr) {
			throw new Zend_Controller_Action_Exception('Group ' . $this->_getParam('key') . ' not found');
		}
		
		$gr->name = $this->_getParam('key');
		$gr->title = $this->_getParam('title');
		$gr->description = $this->_getParam('description');
		$gr->admin = Pidguin_Client::getInstance()->getClient()->user->id;
		$gr->role_id = 3;
		$gr->modified = date('Y-m-d H:i:s');
	
		$rm = Model_Resource::getInstance();
		foreach ($this->_getParam('access') as $resourceId) {
			$select = $rm
			->select()
			->where('id = ?', $resourceId);
			$rr = $rm->fetchRow($select);
			if (!$rr) {
				throw new Zend_Controller_Action_Exception('Resource not found');
			}
		}
		
		$am = $gr->findDependentRowset('Model_Access');
		foreach ($am as $a) {
			$a->delete();
		}
		
		$gr->save();
		$am = Model_Access::getInstance();
		foreach ($this->_getParam('access') as $resourceId) {
			$row = $am->createRow();
			$row->group_id = $gr->id;
			$row->resource_id = $resourceId;
			$row->allow = 1;
			$row->active = 1;
			$row->save();
		}
		$this->_helper->json(array('status' => 'ok'));
	}
}